package com.webserver.controllers;

import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;
import com.webserver.vo.User;

import java.io.*;

/**
 * 处理用户相关业务
 */
public class UserController {
    private static String ueserDir ="./users/";
    static{
        File userDir =new File(ueserDir);
        if (!userDir.exists()){
            userDir.mkdirs();
        }
    }

    public void reg(HttpRequest request, HttpResponse response){
        System.out.println("开始处理用户注册！！！！！！！！！！");
        //1通过request获取用户表单提交的数据
        //获取reg.html中用户名这个输入框的值
        //这里request.getParameter()参数应当与页面上对于输入框的name属性的值
        String username =request.getParameter("username");
        String password =request.getParameter("password");
        String nickname =request.getParameter("nickname");
        String ageStr = request.getParameter("age");
        /**
         * 必要的验证：
         * 要求上述四项信息不能为null；并且年龄还要求必须是一个数字格式
         * 否则直接设置响应对象响应注册失败的提示页面：reg_input_error.html
         * 该页面居中显示一行字：注册失败，输入信息有误
         */
        if(username==null || password==null || nickname==null || ageStr==null||!ageStr.matches("[0-9]+")) {
            File file = new File("./webapps/myweb/reg_input_error.html");
            response.setEntity(file);
            return;
        }
            int age = Integer.parseInt(ageStr);
            System.out.println(username + "," + password + "," + nickname + "," + ageStr);

        /**
         * 判断该用户是否存在，若存在则直接跳转页面：reg_have_user.html
         * 该页面居中显示一行字：该用户已存在，请重新注册
         */
        File userFile =new File(ueserDir + username + ".obj");
        if (userFile.exists()){
            File file =new File("./webapps/myweb/reg_have_user.html");
            response.setEntity(file);
            return;
        }
        //2将该用户写入文件
            try (
                    FileOutputStream fos = new FileOutputStream(userFile);
                    ObjectOutputStream oos = new ObjectOutputStream(fos);
            ) {
                User user = new User(username, password, nickname, age);
                oos.writeObject(user);
                //3设置注册成功页面
                File file = new File("./webapps/myweb/reg_success.html");
                response.setEntity(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        //3设置response响应登录结果页面
    }
    public void login(HttpRequest request,HttpResponse response){
        System.out.println("开始处理登录！！！！！！");
        String username =request.getParameter("username");
        String password =request.getParameter("password");
        File file =new File("./webapps/myweb/login_fail.html");
        if (username==null || password==null){
            response.setEntity(file);
            return;
        }
        File userFile =new File(ueserDir + username + ".obj");
        if (userFile.exists()){
            try (
                    ObjectInputStream ois =new ObjectInputStream(new FileInputStream(userFile));
                    ) {
                    User user =(User)ois.readObject();
                    if (user.getPassword().equals(password)){
                        File loginSuccess=new File("./webapps/myweb/login_success.html");
                        response.setEntity(loginSuccess);
                        return;
                    }
                } catch (IOException | ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
            //登录失败
            response.setEntity(file);

        }
    }
